var e = require;
var t = module;
var n = exports;
var o,
    i =
        (this && this.__extends) ||
        ((o = function (e, t) {
            return (o =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var n in t) Object.prototype.hasOwnProperty.call(t, n) && (e[n] = t[n]);
                })(e, t);
        }),
        function (e, t) {
            function n() {
                this.constructor = e;
            }
            o(e, t), (e.prototype = null === t ? Object.create(t) : ((n.prototype = t.prototype), new n()));
        }),
    r =
        (this && this.__decorate) ||
        function (e, t, n, o) {
            var i,
                r = arguments.length,
                a = r < 3 ? t : null === o ? (o = Object.getOwnPropertyDescriptor(t, n)) : o;
            if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) a = Reflect.decorate(e, t, n, o);
            else
                for (var s = e.length - 1; 0 <= s; s--)
                    (i = e[s]) && (a = (r < 3 ? i(a) : 3 < r ? i(t, n, a) : i(t, n)) || a);
            return 3 < r && a && Object.defineProperty(t, n, a), a;
        },
    a =
        (this && this.__awaiter) ||
        function (e, a, s, c) {
            return new (s = s || Promise)(function (n, t) {
                function o(e) {
                    try {
                        r(c.next(e));
                    } catch (e) {
                        t(e);
                    }
                }
                function i(e) {
                    try {
                        r(c.throw(e));
                    } catch (e) {
                        t(e);
                    }
                }
                function r(e) {
                    var t;
                    e.done
                        ? n(e.value)
                        : ((t = e.value) instanceof s
                              ? t
                              : new s(function (e) {
                                    e(t);
                                })
                          ).then(o, i);
                }
                r((c = c.apply(e, a || [])).next());
            });
        },
    g =
        (this && this.__generator) ||
        function (n, o) {
            var i,
                r,
                a,
                s = {
                    label: 0,
                    sent: function () {
                        if (1 & a[0]) throw a[1];
                        return a[1];
                    },
                    trys: [],
                    ops: []
                },
                e = {next: t(0), throw: t(1), return: t(2)};
            return (
                "function" == typeof Symbol &&
                    (e[Symbol.iterator] = function () {
                        return this;
                    }),
                e
            );
            function t(t) {
                return function (e) {
                    return (function (t) {
                        if (i) throw new TypeError("Generator is already executing.");
                        for (; s; )
                            try {
                                if (
                                    ((i = 1),
                                    r &&
                                        (a =
                                            2 & t[0]
                                                ? r.return
                                                : t[0]
                                                ? r.throw || ((a = r.return) && a.call(r), 0)
                                                : r.next) &&
                                        !(a = a.call(r, t[1])).done)
                                )
                                    return a;
                                switch (((r = 0), (t = a ? [2 & t[0], a.value] : t)[0])) {
                                    case 0:
                                    case 1:
                                        a = t;
                                        break;
                                    case 4:
                                        return s.label++, {value: t[1], done: !1};
                                    case 5:
                                        s.label++, (r = t[1]), (t = [0]);
                                        continue;
                                    case 7:
                                        (t = s.ops.pop()), s.trys.pop();
                                        continue;
                                    default:
                                        if (
                                            !(a = 0 < (a = s.trys).length && a[a.length - 1]) &&
                                            (6 === t[0] || 2 === t[0])
                                        ) {
                                            s = 0;
                                            continue;
                                        }
                                        if (3 === t[0] && (!a || (t[1] > a[0] && t[1] < a[3]))) {
                                            s.label = t[1];
                                            break;
                                        }
                                        if (6 === t[0] && s.label < a[1]) {
                                            (s.label = a[1]), (a = t);
                                            break;
                                        }
                                        if (a && s.label < a[2]) {
                                            (s.label = a[2]), s.ops.push(t);
                                            break;
                                        }
                                        a[2] && s.ops.pop(), s.trys.pop();
                                        continue;
                                }
                                t = o.call(n, s);
                            } catch (e) {
                                (t = [6, e]), (r = 0);
                            } finally {
                                i = a = 0;
                            }
                        if (5 & t[0]) throw t[1];
                        return {value: t[0] ? t[1] : void 0, done: !0};
                    })([t, e]);
                };
            }
        };
Object.defineProperty(n, "__esModule", {value: !0});
var s,
    v = e("index"),
    f = e("events"),
    c = e("sound"),
    l = e("vibrate"),
    u = e("panel-manager"),
    m = e("player-coin"),
    p = e("player-info"),
    b = e("player-war"),
    _ = e("war-cell-item"),
    y = e("war-coin"),
    t = cc._decorator,
    e = t.ccclass,
    t = t.property,
    e =
        ((s = cc.Component),
        i(h, s),
        (h.prototype.start = function () {
            var n = this;
            a(n, void 0, void 0, function () {
                var t;
                return g(this, function (e) {
                    switch (e.label) {
                        case 0:
                            return e.trys.push([0, 2, , 3]), [4, Promise.all([this.loadCells(), this.loadCoin()])];
                        case 1:
                            return (
                                e.sent(),
                                this.setCellSiblingIndex(),
                                this.InitCells(),
                                b.playerWar.setUseADAreaCount(0),
                                [3, 3]
                            );
                        case 2:
                            return (t = e.sent()), console.log("loading item error:", t), [3, 3];
                        case 3:
                            return [2];
                    }
                });
            }),
                this.unlock.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, function (e, t) {
                    "unlocklight" == t.name && (n.unlock.active = !1);
                }),
                (this.tip.active = b.playerWar.getIsGuideLevel());
        }),
        (h.prototype.setIsCanDeal = function (e) {
            this.isCanDeal = e;
        }),
        (h.prototype.getCellScale = function () {
            var e = b.playerWar.getWarLevel();
            return 0 <= e && e < this.cellScales.length ? this.cellScales[e] : this.cellScales[0];
        }),
        (h.prototype.setCellSiblingIndex = function () {}),
        (h.prototype.unlockAdsCell = function (t) {
            var n = this;
            p.playerInfo.setAdsCellLock(t, !1),
                this.cellItems[t] &&
                    this.scheduleOnce(function () {
                        var e = n.cellItems[t].node.convertToWorldSpaceAR(cc.Vec2.ZERO),
                            e = n.unlock.parent.convertToNodeSpaceAR(e);
                        (n.unlock.scale = n.getCellScale()),
                            n.unlock.setPosition(e),
                            (n.unlock.active = !0),
                            n.unlock.getComponent(cc.Animation).play("unlocklight"),
                            n.cellItems[t].unlockAds();
                    }, 0.5);
        }),
        (h.prototype.recoveryCoin = function (e) {
            e.node.removeFromParent(), (e.node.parent = this.node), this.idleCoins.push(e);
        }),
        (h.prototype.chooseCell = function (e, t) {
            if (!this.isMoveing)
                if (this.cellItems && this.cellItems[e]) {
                    if (null != this.oldChooseIdx && this.oldChooseIdx != e && this.cellItems[this.oldChooseIdx]) {
                        var n = this.cellItems[this.oldChooseIdx].getFirstNum(),
                            o = this.cellItems[e].getCanAddCount(),
                            i = this.cellItems[this.oldChooseIdx].getCoinTouchCount(),
                            i = n && n === this.cellItems[e].getFirstNum() && i <= o;
                        if (
                            (n === this.cellItems[e].getFirstNum() &&
                                this.cellItems[this.oldChooseIdx].isOneNumber() &&
                                this.cellItems[e].isOneNumber() &&
                                (i = !0),
                            (i = this.cellItems[e].getCoinItemCount() <= 0 ? !0 : i) && 0 < o)
                        ) {
                            console.log("chooseCell....moveCell");
                            o = this.cellItems[this.oldChooseIdx].getMoveCoin(o);
                            return (
                                this.moveCell(this.oldChooseIdx, e, o),
                                (this.oldChooseIdx = null),
                                void l.vibrate.play(l.VibrateNewType.SHORT)
                            );
                        }
                        var r = this.cellItems[this.oldChooseIdx];
                        r.setChoose(
                            !1,
                            function () {
                                r.setUpBgActvie(!1);
                            },
                            !0
                        ),
                            c.sound.play(c.Sound.WARN),
                            l.vibrate.play(l.VibrateNewType.SHORT),
                            (this.oldChooseIdx = null);
                    } else {
                        c.sound.play(c.Sound.CHOOSECOIN);
                        var a = this.cellItems[e];
                        0 !== a.getCoinItemCount() &&
                            (l.vibrate.play(l.VibrateNewType.SHORT),
                            a.setChoose(!t, function () {
                                t && a.setUpBgActvie(!1);
                            }),
                            (this.oldChooseIdx = t ? null : e));
                    }
                } else console.log("chooseCell error:", e);
        }),
        (h.prototype.moveCell = function (o, i, r, a) {
            var s = this;
            if ((void 0 === a && (a = !0), r || !(r.length <= 0))) {
                this.cellItems[o].setIsNoTouch(!0),
                    this.cellItems[i].setIsNoTouch(!0),
                    this.cellItems[o].setChoose(!1),
                    this.cellItems[i].setChoose(!1);
                for (var c, l, u, p = this, e = r.length - 1; 0 <= e; --e)
                    !(function (e) {
                        var t = r[e],
                            n = t.node.convertToWorldSpaceAR(cc.Vec2.ZERO),
                            n = p.node.convertToNodeSpaceAR(n);
                        t.node.removeFromParent(),
                            (c = p.getCellScale()),
                            (t.node.scale = c),
                            (t.node.parent = p.node),
                            t.node.setPosition(n),
                            (l = p.cellItems[i].getPosition(p.node, e, r.length, c)),
                            (u = p.coinChangeDelayTime - e * p.coinDelayTime),
                            t.moveTo(
                                l,
                                u,
                                e * p.coinDelayTime,
                                function () {
                                    0 === e &&
                                        (s.cellItems[o].setUpBgActvie(!1),
                                        s.cellItems[o].setIsNoTouch(!1),
                                        s.cellItems[i].setIsNoTouch(!1),
                                        s.cellItems[i].addCoins(r),
                                        s.cellItems[i].updateCell(),
                                        s.setMoveing(!1));
                                },
                                a
                            );
                    })(e);
            }
        }),
        (h.prototype.loadCells = function () {
            return a(this, void 0, void 0, function () {
                var t, n, o, i, r, a, s, c, l, u, p, h, d, f, m, y;
                return g(this, function (e) {
                    switch (e.label) {
                        case 0:
                            return (
                                (t = b.playerWar.getWarLevel()),
                                [4, v.ResUtil.loadPrefab("prefab/prefab/war/war-cell-item")]
                            );
                        case 1:
                            for (
                                n = e.sent(),
                                    o = b.playerWar.getArea(t),
                                    i = o.column,
                                    r = o.row,
                                    b.playerWar.getIsGuideLevel() &&
                                        ((i = 1), (r = b.playerWar.getWarGuideCellCount())),
                                    a = this.getCellScale(),
                                    s =
                                        0 <= t && t < this.cellIntervals.length
                                            ? this.cellIntervals[t]
                                            : this.cellIntervals[0],
                                    c = 0.5 * (1 - i),
                                    l = 0.5 * (1 - r),
                                    p = 0;
                                p < i;
                                p++
                            )
                                for (h = 0; h < r; h++)
                                    ((d = 0 == p && 0 == h ? this.itemBg : cc.instantiate(this.itemBg)).parent =
                                        this.cellParent),
                                        (d.active = !0),
                                        (d.scale = a),
                                        d.setPosition((l + h) * s.x, (c + p) * s.y);
                            for (
                                u = this.itemBg.height * a * 0.5 + this.bgMask.height * a * 0.5, p = i - 1;
                                0 <= p;
                                p--
                            ) {
                                for (1, h = 0; h < r; h++)
                                    ((d = cc.instantiate(n)).parent = this.cellParent),
                                        (d.scale = a),
                                        d.setPosition((l + h) * s.x, (c + p) * s.y),
                                        (f = d.getComponent(_.default)),
                                        this.cellItems.push(f),
                                        ((m = cc.instantiate(this.selectNode)).parent = this.selectNode.parent),
                                        (m.scale = a),
                                        m.setPosition((l + h) * s.x, (c + p) * s.y),
                                        f.setSelectNode(m);
                                ((y = cc.instantiate(this.bgMask)).parent = this.cellParent),
                                    (y.active = !0),
                                    (y.scaleY = a),
                                    y.setPosition(0, (c + p) * s.y - u);
                            }
                            return [2];
                    }
                });
            });
        }),
        (h.prototype.loadCoin = function () {
            return a(this, void 0, void 0, function () {
                var t;
                return g(this, function (e) {
                    switch (e.label) {
                        case 0:
                            return (t = this), [4, v.ResUtil.loadPrefab("prefab/prefab/war/war-coin")];
                        case 1:
                            return (t.coinPrefab = e.sent()), [2];
                    }
                });
            });
        }),
        (h.prototype.InitCells = function (e) {
            void 0 === e && (e = !1);
            var t = new Map();
            if (!b.playerWar.getIsGuideLevel()) {
                var n = b.playerWar.getWarLevel(),
                    o = b.playerWar.getArea(n),
                    n = o.column * o.row;
                if (this.cellItems.length !== n)
                    return void console.log("InitCells load count error:", this.cellItems.length, n);
                for (var i = o.adArea1.split("#"), r = 0; r < i.length; r++) {
                    var a = i[r],
                        s = Number(a);
                    s && 0 < s && t.set(s, !0);
                }
            }
            for (r = 0; r < this.cellItems.length; r++)
                (a = this.cellItems[r]), t.has(r) && a.setIsAds(!0), a.init(r, e, this.coinPrefab, [], function () {});
            this.isCanDeal && this.dealCoins();
        }),
        (h.prototype.dealCoins = function () {
            var e,
                u = this,
                t = new Map();
            t = b.playerWar.getIsGuideLevel()
                ? b.playerWar.getGuideData()
                : ((e = b.playerWar.getWarLevel()), b.playerWar.getInitData(e));
            var p = this.getCellScale(),
                h = ((e = b.playerWar.getWarLevel()), b.playerWar.getArea(e).row);
            b.playerWar.getIsGuideLevel() && (h = b.playerWar.getWarGuideCellCount());
            var d = 0;
            c.sound.play(c.Sound.TZ_GIVECOIN),
                t.forEach(function (n, o) {
                    var i = [];
                    o % h == 0 && d++;
                    for (var r, a, s, c, l = (d - 1) * u.dealCoinDelayTime, e = 0; e < n.length; e++)
                        !(function (e) {
                            var t = n[e];
                            ((r = cc.instantiate(u.coinPrefab)).parent = u.startNode),
                                (r.active = !0),
                                r.setPosition(0, 0),
                                (a = r.getComponent(y.default)),
                                ((s = new m.coinData()).cellIdx = o),
                                (s.num = t.num),
                                (c = u.cellItems[s.cellIdx].getPosition(u.startNode, n.length - e - 1, n.length, p)),
                                a.init(s),
                                i.unshift(a),
                                (r.scale = p),
                                a.moveToCubic(c, u.dealCoinTime, l, function () {
                                    e === n.length - 1 &&
                                        (u.cellItems[s.cellIdx].addCoins(i),
                                        u.cellItems[s.cellIdx].updateCell(),
                                        l == (d - 1) * u.dealCoinDelayTime &&
                                            (u.setMoveing(!1),
                                            f.operEvent.emit({action: f.default.startWarTime, data: {}})));
                                });
                        })(e);
                });
        }),
        (h.prototype.checkFinish = function () {
            for (var e = !1, t = 0; t < this.cellItems.length; t++) {
                var n = this.cellItems[t];
                if (!n.isCanMerge() && 0 !== n.getCoinItemCount()) {
                    e = !0;
                    break;
                }
            }
            e ||
                (f.operEvent.emit({action: f.default.saveWarTime, data: {}}),
                b.playerWar.getIsGuideLevel()
                    ? (b.playerWar.setIsGuideLevel(!1),
                      f.operEvent.emit({action: f.default.restartCellLayer, data: {isGuide: !0}}))
                    : (v.platform.stopVideoRecord(), u.PanelManager.ins.open("prefab/panel/warSucPanel", null)));
        }),
        (h.prototype.setMoveing = function (e) {
            (this.isMoveing = e) || this.checkFinish();
        }),
        r([t(cc.Node)], h.prototype, "cellParent", void 0),
        r([t(cc.Node)], h.prototype, "startNode", void 0),
        r([t(cc.Node)], h.prototype, "selectNode", void 0),
        r([t(cc.Node)], h.prototype, "itemBg", void 0),
        r([t(cc.Node)], h.prototype, "unlock", void 0),
        r([t(cc.Node)], h.prototype, "bgMask", void 0),
        r([t(cc.Node)], h.prototype, "tip", void 0),
        r([t([cc.Vec2])], h.prototype, "cellIntervals", void 0),
        r([t([cc.Float])], h.prototype, "cellScales", void 0),
        r([t({displayName: "金币切换格子移动时间"})], h.prototype, "coinChangeDelayTime", void 0),
        r([t({displayName: "金币移动之间的延迟"})], h.prototype, "coinDelayTime", void 0),
        r([t({displayName: "发金币移动时间"})], h.prototype, "dealCoinTime", void 0),
        r([t({displayName: "发金币移动之间的延迟"})], h.prototype, "dealCoinDelayTime", void 0),
        r([t({displayName: "金币 洗牌 切换格子移动时间"})], h.prototype, "coinClearChangeDelayTime", void 0),
        r([t({displayName: "金币 洗牌 移动之间的延迟"})], h.prototype, "coinClearDelayTime", void 0),
        r([e], h));
function h() {
    var e = (null !== s && s.apply(this, arguments)) || this;
    return (
        (e.cellParent = null),
        (e.startNode = null),
        (e.selectNode = null),
        (e.itemBg = null),
        (e.unlock = null),
        (e.bgMask = null),
        (e.tip = null),
        (e.cellIntervals = []),
        (e.cellScales = []),
        (e.coinChangeDelayTime = 1),
        (e.coinDelayTime = 0.05),
        (e.dealCoinTime = 1),
        (e.dealCoinDelayTime = 0.05),
        (e.coinClearChangeDelayTime = 2),
        (e.coinClearDelayTime = 0.1),
        (e.cellItems = []),
        (e.oldChooseIdx = null),
        (e.isMoveing = !0),
        (e.idleCoins = []),
        (e.coinPrefab = null),
        (e.isCanDeal = !1),
        e
    );
}
n.default = e;
